package com.power.sifa.std.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.std.model.Std_attach;
import com.power.sifa.std.model.pageInfo.Std_attachPageInfo;
import com.power.sifa.std.api.IStd_attachFeign;
import com.power.sifa.std.service.Std_attachService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   申请所需材料库控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Std_attachController",tags={"申请所需材料库"})
public class Std_attachController implements IStd_attachFeign{

    @Resource
    private Std_attachService std_attachService;


    @Override
    @ApiOperation("查询申请所需材料库分页列表")
    @SaCheckPermission("std_attach_list")
    public ResponseDTO list(Std_attachPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Std_attach> page = this.std_attachService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Std_attach std_attach : page) {
                std_attach.geneText();
                list.add(std_attach);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看申请所需材料库")
    @SaCheckPermission("std_attach_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Std_attach std_attach  = std_attachService.getById(id);
        if (std_attach != null) {
            std_attach.geneText();
        }
        return ResponseDTO.succData(std_attach);
    }

    @Override
    @ApiOperation("删除申请所需材料库")
    @SaCheckPermission("std_attach_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        std_attachService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改申请所需材料库")
    @SaCheckPermission("std_attach_save")
    public ResponseDTO save(Std_attach std_attach) {
        Assert.notNull(std_attach, "std_attach不能为空");
        std_attachService.saveOrUpdate(std_attach);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除申请所需材料库")
    @SaCheckPermission("std_attach_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        std_attachService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("std_attach_list")
    public void output(HttpServletResponse response, Std_attachPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "申请所需材料库";
        String[] heads = {"序号","名称","描述说明","附件支持类型","模板名称"};
        String[] fields = {"id","name","describe","type","tplfile"};
        Page<Std_attach> page = this.std_attachService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "申请所需材料库";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","名称","描述说明","附件支持类型","模板名称"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","name","describe","type","tplfile"};
        List<Std_attach> list = (List<Std_attach>)ExcelUtil.Import(fields, file, Std_attach.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.std_attachService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
